package com.thymeleaf.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.thymeleaf.demo.dto.LoginDto;
import com.thymeleaf.demo.lib.AdminLib;
import com.thymeleaf.demo.model.MallAdmin;
import com.thymeleaf.demo.myexception.MyExcption;
import com.thymeleaf.demo.service.LoginService;
import com.thymeleaf.demo.util.result.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class LoginServiceImpl implements LoginService {
    @Autowired
    MallAdminServiceImpl adminService;
    @Autowired
    AdminLib adminLib;

    @Override
    public MallAdmin doLogin(LoginDto loginDto) throws MyExcption {
        QueryWrapper<MallAdmin> wrapper = new QueryWrapper<>();
        // 根据账号查询用户
        wrapper.eq("account", loginDto.getAccount());
        MallAdmin admin = adminService.getOne(wrapper);
        if(admin == null){
            // 账号错误
            throw new MyExcption(ResultEnum.USERNAME_ERROR.getCode());
        }
        if(!admin.getPwd().equals(adminLib.getPwd(loginDto.getPwd(), admin.getSalt()))){
            // 密码错误
            throw new MyExcption(ResultEnum.PWD_ERROR.getCode());
        }

        return admin;
    }
}
